﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminDisciplines : Form
    {
        private aDbDataSet.DisciplinesRow drActiveDiscipline;
        private bool bNew;
        
        // Constructor
        public frmAdminDisciplines()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Load
        private void frmAdminDisciplines_Load(object sender, EventArgs e)
        {
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            this.disciplinesTableAdapter.Fill(this.aDbDataSet.Disciplines);
        }

        // Selection changed
        private void disciplinesDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (disciplinesDataGridView.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)disciplinesDataGridView.CurrentRow.DataBoundItem;
                chkActive.Checked = Convert.ToBoolean(drv["Active"]);
                nudOrder.Value = Convert.ToInt32(drv["Order"]);
                txtName.Text = drv["Name"].ToString();
                txtAlterName.Text = drv["AlterName"].ToString();
                txtDesc.Text = drv["ImagePath"].ToString();
                nudCode.Value = Convert.ToInt32(drv["Code"]);
                nudResultCode.Value = Convert.ToInt32(drv["ResultCode"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Disciplíny", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                chkActive.Checked = true;
                nudOrder.Value = 1;
                txtName.Text = string.Empty;
                txtAlterName.Text = string.Empty;
                txtDesc.Text = string.Empty;
                nudCode.Value = 1;
                nudResultCode.Value = 1;

                this.drActiveDiscipline = aDbDataSet.Disciplines.NewDisciplinesRow();

                this.drActiveDiscipline["HasStats"] = true;
                this.drActiveDiscipline["Active"] = true;
                this.drActiveDiscipline["Sync"] = false;
                this.drActiveDiscipline["WebId"] = 0;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Disciplíny - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                DataRowView drv = (DataRowView)disciplinesDataGridView.CurrentRow.DataBoundItem;
                this.drActiveDiscipline = (aDbDataSet.DisciplinesRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Disciplíny - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            disciplinesDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte názov!", "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                this.drActiveDiscipline["Active"] = chkActive.Checked;
                this.drActiveDiscipline["Order"] = nudOrder.Value;
                this.drActiveDiscipline["Name"] = txtName.Text;
                this.drActiveDiscipline["AlterName"] = txtAlterName.Text;
                this.drActiveDiscipline["ImagePath"] = txtDesc.Text;
                this.drActiveDiscipline["Code"] = nudCode.Value;
                this.drActiveDiscipline["ResultCode"] = nudResultCode.Value;
                this.drActiveDiscipline["Modified"] = DateTime.Now;

                if (bNew)
                {
                    aDbDataSet.Disciplines.Rows.Add(this.drActiveDiscipline);
                    this.disciplinesTableAdapter.Update(this.aDbDataSet.Disciplines);
                }
                else
                {
                    this.disciplinesTableAdapter.Update(this.drActiveDiscipline);
                }

                this.disciplinesTableAdapter.Fill(this.aDbDataSet.Disciplines);

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;

                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Disciplíny - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
